<template>
    <div class="bottom-bar">
        <div class="play-bar play-bar-unlock">
            <div class="updown">
                <div class="left">
                    <a href="javascript:;" class="btn"></a>
                </div>
                <div class="right"></div>
            </div>
            <div class="bg"></div>
            <div class="hand" title="展开播放条"></div>
            <div class="wrap">
                <div class="btns">
                    <a href="javascript:;" hidefocus="true" class="prv" title="上一首(ctrl+←)" @click="preHandler">上一首</a>
                    <a href="javascript:;" hidefocus="true" class="ply" :class="isPlaying ? 'pas' : ''" title="播放/暂停(p)"
                        @click="togglePlayer">播放/暂停</a>
                    <a href="javascript:;" hidefocus="true" class="nxt" title="下一首(ctrl+→)" @click="nextHandler">下一首</a>

                </div>
                <div class="head">
                    <img :src="playingMusicImgUrlPrefix + (currentPlayMusic && currentPlayMusic.playingImgUrl)">
                    <a href="javascript:;" hidefocus="true" class="msk"></a>
                </div>
                <div class="play-progress-bar">
                    <div class="words">
                        <router-link hidefocus="true" class="f-thide name f-fl"
                            :title="currentPlayMusic && currentPlayMusic.name">{{ currentPlayMusic && currentPlayMusic.name }}</router-link>
                        <router-link to="/mv?id=14256556" class="mv f-fl" title="MV"></router-link>
                        <span class="by f-thide f-fl">
                            <span class="author" tilte="auhtor-name">
                                <template v-for="(item, index) in currentPlayMusic ? currentPlayMusic.artists : []" key="index">
                                    <router-link :to="item.artistAddress" hidefocus="true">{{ item.name }}</router-link>
                                    <span v-if="index < currentPlayMusic.artists.length - 1"> / </span>
                                </template>
                            </span>
                        </span>
                        <a href="/discover/toplist?id=3779629&amp;_hash=songlist-2686958558" class="src"
                            title="来自榜单"></a>
                    </div>
                    <div class="pbar" @click="selectProgressPos" ref="progressBar">
                        <div class="barbg">
                            <div class="grey-bar" style="width: 0%;"></div>
                            <div class="cur" :style="{ width: currentPercent + '%' }">
                                <span class="btn f-tdn">
                                    <i></i>
                                </span>
                            </div>
                        </div>
                        <span class="time">
                            <em>{{ currentTime }}</em>
                            / {{ totalTime }}
                        </span>

                    </div>
                </div>

                <div class="oper f-fl">
                    <a href="javascript:;" hidefocus="true" class="icn icn-pip" title="画中画歌词">画中画歌词</a>
                    <a href="javascript:;" hidefocus="true" class="icn icn-add" title="收藏">收藏</a>
                    <a href="javascript:;" hidefocus="true" class="icn icn-share" title="分享">分享</a>
                </div>

                <div class="ctrl f-fl">
                    <div class="volumn" style="visibility: hidden;">
                        <div class="barbg"></div>
                        <div class="vbg">
                            <div class="curr" style="height: 74px;"></div>
                            <div class="btn" style="top: 16px"> </div>
                        </div>
                    </div>

                    <a href="javascript:;" hidefocus="true" class="icn icn-vol"></a>
                    <a href="javascript:;" hidefocus="true" class="icn icn-loop"></a>
                    <span class="add f-pr">
                        <span class="tip" style="display: none;">已添加到播放列表</span>
                        <a href="javascript:;" hidefocus="true" title="播放列表"
                            class="icn icn-list s-fc3">{{ musicStore.play_list.length }}</a>
                    </span>
                    <a href="javascript:;" hidefocus="true" class="icn icn-audioquality"></a>
                    <div class="tip tip-1" style="display: none;">循环</div>
                </div>
            </div>
            <PlayListMenu ref="PlayListMenuComponent" :lyricData="lyricData" :playListData="playListData" :currentPlayTime="currentPlayTime">
            </PlayListMenu>
        </div>
        <audio :src="musicResUrlPrefix + (currentPlayMusic ? currentPlayMusic.path : 'october')+'.m4a'" id="audioControl" ref="audioControl"></audio>
        <!-- <audio src="/src/assets/music_res/october.m4a" id="audioControl" ref="audioControl"></audio> -->
    </div>
</template>
<script setup>
import { inject, nextTick, onMounted, onUpdated, provide, ref, watch } from 'vue';
import { useMusicStore } from '../stores/music';
import PlayListMenu from './PlayListMenu.vue';
import lyricData from '@/json_data/play_list_menu/lyric_data.json'
import playListData from "@/json_data/play_list_menu/play_list_data.json"
import { reqMusic } from '../request/api';

const musicStore = useMusicStore();
const audioControl = ref();
const progressBar = ref();
const isPlaying = ref(false);
const totalTime = ref('00:00');
const currentTime = ref('00:00');
const currentPlayTime = ref(0);
const currentPercent = ref(0);
const PlayListMenuComponent = ref();

const currentPlayMusic = ref(null);
let loadFirst = true;

let playingMusicImgUrlPrefix = "http://localhost:8080/api/music/playingImg/";
let musicResUrlPrefix = "http://localhost:8080/api/music/sendMusic/"
const currentMusicIndex = ref(0);



provide('currentPlayMusic', currentPlayMusic);
provide('currentMusicIndex',currentMusicIndex);
provide('currentPlayTime',currentPlayTime);

musicStore.fetchPlayList().then(async ()=>{
    console.log('play_list_map_table',musicStore.play_list_map_table);
    currentPlayMusic.value = await reqMusic(musicStore.play_list_map_table[currentMusicIndex.value]);
    console.log(currentPlayMusic.value);
})



watch(()=>currentPlayMusic.value, function(){
    nextTick(()=>{
        PlayListMenuComponent.value.caculateLyricUIState();
    })
    
    if(loadFirst) return loadFirst = false
    currentPercent.value = 0;
    if (audioControl.value.paused) return;
    nextTick(()=>{
        audioControl.value.play();
    })
})



const selectProgressPos = ($event) => {
    let pbarWidth = parseFloat(getComputedStyle(progressBar.value).getPropertyValue('width'));
    let ratio = $event.offsetX / pbarWidth;
    currentPercent.value = ratio * 100;
    audioControl.value.currentTime = ratio * audioControl.value.duration;
}

const togglePlayer = () => {
    
    if (audioControl.value.paused) {
        isPlaying.value = true;
        audioControl.value.play().catch(e => console.error("play fail", e));
    } else {
        audioControl.value.pause();
        isPlaying.value = false;
    }

    console.log('after isPaused', audioControl.value.paused);

}

const nextHandler = async () => {
    currentMusicIndex.value >= musicStore.play_list_map_table.length - 1 ? currentMusicIndex.value = 0 : currentMusicIndex.value++;
    currentPlayMusic.value = currentPlayMusic.value = await reqMusic(musicStore.play_list_map_table[currentMusicIndex.value]);
}

const preHandler = async () => {
    currentMusicIndex.value <= 0 ? currentMusicIndex.value = musicStore.play_list_map_table.length - 1 : currentMusicIndex.value--;
    currentPlayMusic.value = await reqMusic(musicStore.play_list_map_table[currentMusicIndex.value]);
}

onMounted(() => {
    audioControl.value.addEventListener('loadedmetadata', function () {
        totalTime.value = formatTime(audioControl.value.duration);
    })

    audioControl.value.addEventListener('timeupdate', function () {
        currentPlayTime.value = audioControl.value.currentTime;
        currentTime.value = formatTime(audioControl.value.currentTime);
        currentPercent.value = audioControl.value.currentTime / audioControl.value.duration * 100;
    })

    audioControl.value.addEventListener('error', function () {
        console.error('fail load', audioControl.value.error);
    })

})


const formatTime = (time) => {
    const min = parseInt(time / 60);
    const sec = parseInt(time % 60);
    return (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec);
}



</script>
<style lang="scss" scoped>
.bottom-bar {
    position: fixed;
    zoom: 1;
    bottom: 0;
    left: 0;
    right: 0;
    height: 0;
    width: 100%;
    z-index: 1002;


    .play-bar-lock {
        .updown {
            .left {
                .btn {
                    background-position: -100px -380px;

                    &:hover {
                        background-position: -100px -400px;
                    }
                }

            }

        }


    }

    .play-bar-unlock {
        .updown {
            .left {
                .btn {
                    background-position: -80px -380px;

                    &:hover {
                        background-position: -80px -400px;
                    }
                }
            }

        }
    }

    .play-bar {
        position: absolute;
        zoom: 1;
        top: -53px;
        left: 0;
        width: 100%;
        height: 53px;
        margin: 0 auto;

        .icn {
            float: left;
            width: 25px;
            height: 25px;
            margin: 11px 2px 0 0;
            text-indent: -9999px;
        }

        .updown {
            position: relative;
            z-index: 11;

            .left {
                position: absolute;
                top: -14px;
                right: 15px;
                width: 52px;
                height: 67px;
                background-position: 0 -380px;

                .btn {
                    display: block;
                    width: 18px;
                    height: 18px;
                    margin: 6px 0 0 17px;

                }
            }

            .right {
                position: absolute;
                top: -1px;
                right: 0;
                width: 15px;
                height: 54px;
                background-position: -52px -393px;
                pointer-events: none;
            }


        }

        .bg {
            height: 53px;
            zoom: 1;
            margin-right: 67px;
            background-position: 0 0;
            background-repeat: repeat-x;
        }

        .hand {
            position: absolute;
            top: -10px;
            width: 100%;
            height: 20px;
            cursor: pointer;
        }

        .wrap {
            position: absolute;
            margin-left: -523px !important;
            left: 50%;
            top: 6px;
            z-index: 15;
            width: 1030px;
            height: 47px;
            margin: 0 auto;

            .btns {
                width: 137px;
                padding: 6px 0 0 0;
                float: left;

                a {
                    display: block;
                    float: left;
                    width: 28px;
                    height: 28px;
                    margin-right: 8px;
                    margin-top: 5px;
                    text-indent: -9999px;
                }

                .prv {
                    background-position: 0 -130px;

                    &:hover {
                        background-position: -30px -130px;
                    }
                }



                .ply {
                    width: 36px;
                    height: 36px;
                    margin-top: 0;
                    background-position: 0 -204px;

                    &:hover {
                        background-position: -40px -204px;
                    }
                }

                .pas {
                    background-position: 0 -165px;

                    &:hover {
                        background-position: -40px -165px;
                    }
                }

                .nxt {
                    background-position: -80px -130px;

                    &:hover {
                        background-position: -110px -130px;
                    }
                }
            }

            .head {
                position: relative;
                float: left;
                margin: 6px 15px 0 0;
                width: 34px;
                height: 34px;

                img {
                    height: 34px;
                    width: 34px;
                }

                .msk {
                    position: absolute;
                    top: 0;
                    left: 0;
                    display: block;
                    width: 34px;
                    height: 35px;
                    background-position: 0 -80px;
                }
            }

            .play-progress-bar {
                width: 581px;
                position: relative;
                float: left;

                .words {
                    height: 28px;
                    overflow: hidden;
                    color: #e8e8e8;
                    text-shadow: 0 1px 0 #171717;
                    line-height: 28px;

                    .mv{
                        width: 19px;
                        height: 17px;
                        margin: 6px 0 0 3px;
                        background-position: 0 -57px;

                        &:hover{
                            background-position: -20px -57px;
                        }
                    }
                    

                    .name {
                        max-width: 300px;
                        color: #e8e8e8;

                    }

                    .by {
                        max-width: 220px;
                        margin-left: 15px;
                        color: #9b9b9b;

                        a {
                            color: #9b9b9b;
                        }
                    }

                    .src {
                        float: left;
                        width: 14px;
                        height: 15px;
                        margin: 7px 0 0 13px;
                        background-position: -110px -103px;

                        &:hover {
                            background-position: -130px -103px;
                        }
                    }
                }

                .pbar {
                    width: 466px;
                    position: relative;

                    .barbg {
                        width: 466px;
                        height: 9px;
                        background-position: right 0;

                        .grey-bar {
                            height: 9px;
                            background-position: right -30px;
                        }

                        .cur {
                            position: absolute;
                            top: 0;
                            left: 0;
                            background-position: left -66px;
                            height: 9px;

                            .btn {
                                position: absolute;
                                top: -7px;
                                right: -13px;
                                width: 22px;
                                height: 24px;
                                margin-left: 11px;
                                background-position: 0 -250px;

                                &:hover {
                                    background-position: 0 -280px;
                                }

                                i {
                                    visibility: hidden;
                                    position: absolute;
                                    left: 5px;
                                    top: 5px;
                                    width: 12px;
                                    height: 12px;
                                    background: url(../assets/loading.gif);
                                }
                            }
                        }
                    }

                    .time {
                        position: absolute;
                        top: -3px;
                        right: -84px;
                        color: #797979;
                        text-shadow: 0 1px 0 #121212;

                        em {
                            color: #a1a1a1;
                        }
                    }
                }
            }

            .oper {

                width: 87px;
                float: left;



                .icn-pip {
                    position: relative;
                    background: url(../assets/icn_pip.png) no-repeat 0 0;

                    &:hover {
                        background-position-y: -25px;
                    }
                }

                .icn-add {
                    background-position: -88px -163px;

                    &:hover {
                        background-position: -88px -189px;
                    }
                }

                .icn-share {
                    background-position: -114px -163px;

                    &:hover {
                        background-position: -114px -189px;
                    }
                }
            }

            .ctrl {
                position: relative;
                z-index: 10;
                width: 160px;
                padding-left: 13px;
                background-position: -147px -238px;

                .volumn {
                    position: absolute;
                    top: -113px;
                    left: 9px;
                    clear: both;
                    width: 32px;
                    height: 113px;

                    .barbg {
                        position: absolute;
                        top: 0;
                        left: 0;
                        width: 32px;
                        height: 113px;
                        background-position: 0 -503px;
                    }

                    .vbg {
                        position: absolute;
                        top: 7px;
                        padding: 4px 0;
                        left: 14px;
                        width: 4px;
                        height: 93px;

                        .curr {
                            position: absolute;
                            width: 4px;
                            top: auto;
                            bottom: 4px;
                            left: 0;
                            background-position: -40px bottom;
                            overflow: hidden;
                        }

                        .btn {
                            position: absolute;
                            top: 0;
                            left: -7px;
                            display: block;
                            width: 18px;
                            height: 20px;
                            background-position: -40px -250px;
                            cursor: pointer;
                        }
                    }

                }

                .icn-vol {
                    background-position: -2px -248px;

                    &:hover {
                        background-position: -31px -248px;
                    }
                }

                .icn-loop {
                    background-position: -3px -344px;

                    &:hover {
                        background-position: -33px -344px;
                    }
                }

                .add {
                    float: left;
                    width: 59px;
                    height: 36px;
                    position: relative;

                    .tip {
                        position: absolute;
                        top: -51px;
                        left: -65px;
                        clear: both;
                        width: 152px;
                        height: 49px;
                        background-position: 0 -287px;
                        text-align: center;
                        color: #fff;
                        line-height: 37px;
                    }

                    .icn-list {
                        display: block;
                        float: none;
                        width: 38px;
                        padding-left: 21px;
                        background-position: -42px -68px;
                        line-height: 27px;
                        text-align: center;
                        color: #666;
                        text-shadow: 0 1px 0 #080707;
                        text-indent: 0;
                        text-decoration: none;

                        &:hover {
                            background-position: -42px -98px;
                        }
                    }
                }

                .icn-audioquality {
                    background: url(../assets/audio-quality.png);
                    background-size: 45px 30px;
                    margin-left: 4px;
                    width: 41px;
                    margin-top: 9px;

                    &:hover {
                        background: url(../assets/audio-quality-hover.png);
                        background-size: 45px 30px;
                    }
                }

                .tip-1 {
                    top: -35px;
                    left: 12px;
                    width: 81px;
                    height: 39px;
                    line-height: 34px;
                    background-position: 0 -457px;
                }
            }


        }
    }
}
</style>